#!/usr/bin/python

#-------------------------------------------------------------------------------
# Name:        DisableWindowsErrorReporting
# Purpose:     To disable the WE Reporting that hangs processes during crash
#              preventing them from exiting and halting the script. Makes 2
#              registry edits and an attempt to pass a command.
#
# Author:      Donald L. Talbert
#
# Created:     25/09/2012
# Copyright:   (c) Donald L. Talbert 2012
# Licence:     MIT
#-------------------------------------------------------------------------------

from _winreg import *
#import subprocess

def SetKey1():
    """
    Set first Windows Error Reporting Registry Key to Disabled.
    """
    keyVal = r'SYSTEM\ControlSet001\Control\Windows'
    key = OpenKey(HKEY_LOCAL_MACHINE, keyVal, 0, KEY_ALL_ACCESS)
    SetValueEx(key, "ErrorMode", 0, REG_DWORD, 2) # 2 disables this key oddly.
    CloseKey(key)
    return True

def SetKey2():
    """
    Sets second Windows Error Reporting Key to Disabled.
    """
    keyVal = r'Software\Microsoft\Windows\Windows Error Reporting'
    key = OpenKey(HKEY_CURRENT_USER, keyVal, 0, KEY_ALL_ACCESS)
    SetValueEx(key, "DontShowUI", 0, REG_DWORD, 1) # Sets to 1 to disable
    CloseKey(key)
    return True

#def SetCommand():
    #"""
    #Attempts to run a command that on some systems will disable error reporting.
    #"""
    #subprocess.Popen('serverWerOptin /disable', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()
    #return None

def Main():
    """
    The Main launcher. Sets all the values.
    """
    SetKey1()
    SetKey2()
    print "Registry is now set"
    #SetCommand()


if __name__ == '__main__':
    Main()

